All Objects
OBJECT: Date
new
Date
()>
new
Date
(milliseconds)
new
Date
(dateString)
new
Date
(yr_num, mo_num, day_num [, hr_num, min_num,
sec_num, ms_num])
The
Date
object allows you to work programatically with dates and times. You create a
Date
object using the Date constructor as shown in the syntax above and the available parameters are as follows:
milliseconds
- an integer that represents the number of milliseconds since 01/01/70 00:00:00.
dateString
- a string that represents the date in a format that is recognized by the
Date.parse
method.
yr_num, mo_num, day_num
- an integer that represents the year, month or day of the date.
hr_num, min_num, sec_num, ms_num
- an integer that represents the hours, minutes, seconds and milliseconds.
If you don't supply any of the above parameters, JavaScript creates an object for today's date according to the time on the local machine. If any arguments are supplied, you have to include the year, month and day as a minimum, with the time parameters being optional. Note that if you only supply some arguments, any not supplied are set to 0.
All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds. The range of a
Date
object relative to 01/01/1970 (UTC) is -100,000,000 to 100,000,000 days and both Universal (UTC) time and Greenwich Mean Time (GMT) are supported.
The following code uses
Date
objects to calculate the time remaining, in days, to the start of the next millennium.
Code:
d = new Date() //today's date
mill=new Date(3000, 00, 01, 00, 00, 00) //Next millennium start date
diff = mill-d //difference in milliseconds
mtg = new String(diff/86400000) //calculate days and convert to string
point=mtg.indexOf(".") //find the decimal point
days=mtg.substring(0,point) //get just the whole days
document.write("There are only " + days + " days remaining to the start of the next millennium.")
Output:
There are only 365033 days remaining to the start of the next millennium.
PROPERTIES
constructor Property
This property returns a reference to the function that created the
Date
object's prototype.
Syntax:
object.
constructor
prototype Property
This property represents the prototype for the object's class and can be used to add properties and methods to all instances of that class.
Syntax:
object.
prototype
METHODS
getDate Method
This method returns an integer (between 1 and 31) representing the day of the month for the specified (local time) date.
Syntax:
object.
getDate( )
getDay Method
This method returns an integer (0 for Sunday thru 6 for Saturday) representing the day of the week.
Syntax:
object.
getDay( )
getFullYear Method
This method returns an integer representing the year of a specified date. The integer returned is a four digit number, 1999, for example, and this method is to be prefered over getYear.
Syntax:
object.
getFullYear( )
getHours Method
This method returns an integer between 0 and 23 that represents the hour (local time) for the specified date.
Syntax:
object.
getHours( )
getMilliseconds Method
This method returns an integer between 0 and 999 that represents the milliseconds (local time) for the specified date.
Syntax:
object.
getMilliseconds( )
getMinutes Method
This method returns an integer between 0 and 59 that represents the minutes (local time) for the specified date.
Syntax:
object.
getMinutes( )
getMonth Method
This method returns an integer (0 for January thru 11 for December) that represents the month for the specified date.
Syntax:
object.
getMonth( )
getSeconds Method
This method returns an integer between 0 and 59 that represents the seconds (local time) for the specified date.
Syntax:
object.
getSeconds( )
getTime Method
This method returns a numeric value representing the number of milliseconds since midnight 01/01/1970 for the specified date.
Syntax:
object.
getTime( )
getTimezoneOffset Method
This method returns the difference in minutes between local time and Greenwich Mean Time. This value is not a constant, as you might think, because of the practice of using Daylight Saving Time.
Syntax:
object.
getTimezoneOffset( )
getUTCDate Method
This method returns an integer between 1 and 31 that represents the day of the month, according to universal time, for the specified date.
Syntax:
object.
getUTCDate( )
getUTCDay Method
This method returns an integer (0 for Sunday thru 6 for Saturday) that represents the day of the week, according to universal time, for the specified date.
Syntax:
object.
getUTCDay( )
getUTCFullYear Method
This method returns a four-digit absolute number that represents the year, according to universal time, for the supplied date.
Syntax:
object.
getUTCFullYear( )
getUTCHours Method
This method returns an integer between 0 and 23 that represents the hours, according to universal time, in the supplied date.
Syntax:
object.
getUTCHours( )
getUTCMilliseconds Method
This method returns an integer between 0 and 999 that represents the milliseconds, according to universal time, in the specified date.
Syntax:
object.
getUTCMilliseconds( )
getUTCMinutes Method
This method returns an integer between 0 and 59 that represents the minutes, in universal time, for the supplied date.
Syntax:
object.
getUTCMinutes( )
getUTCMonth Method
This method returns an integer, 0 for January thru 11 for December, according to universal time, for the specified date.
Syntax:
object.
getUTCMonth
getUTCSeconds Method
This method returns an integer between 0 and 59 that represents the seconds, according to universal time, for the specified date.
Syntax:
object.
getUTCSeconds( )
parse
Method
This method returns takes a date string and returns the number of milliseconds since January 01 1970 00:00:00.
Syntax:
Date.
parse
dateString
setDate Method
This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to local time.
Syntax:
object.
setDate
(dateVal)
setFullYear
Method
This method is used to set the full year for the supplied date according to local time.
Syntax:
object.
setFullYear
(yearVal [, monthVal, dayVal])
setHours
Method
This method is used to set the hours for the supplied date according to local time.
Syntax:
object.
setHours
(hoursVal [, minutesVal, secondsVal, msVal])
setMilliseconds Method
This method is used to set the milliseconds for the supplied date according to local time. The
millisecondsVal
parameter expects a number between 0 and 999 athough if this is exceeded, the
setMilliseconds
method will automatically increment other values in the
Date
object, e.g. if 1020 is specified, the seconds value is incremented by one and
millisecondsVal
is set to 20.
Syntax:
object.
setMilliseconds
(millisecondsVal)
setMinutes
Method
This method is used to set the minutes for the supplied date according to local time.
Syntax:
object.
setMinutes
(minutesVal [, secondsVal, msVal])
setMonth
Method
This method is used to set the month for the supplied date according to local time.
Syntax:
object.
setMonth
(monthVal [, dayVal])
setSeconds
Method
This method is used to set the seconds for the specified date according to local time.
Syntax:
object.
setSeconds
(secondsVal [, msVal)
setTime Method
This method is used to set the time of a
Date
object according to local time. The
timeVal
argument is an integer that represents the number of milliseconds elapsed since 1 January 1970 00:00:00.
Syntax:
object.
setTime
(timeVal)
setUTCDate Method
This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to universal time.
Syntax:
object.
setUTCDate
(dateVal)
setUTCFullYear
Method
This method is used to set the full year for the supplied date according to universal time.
Syntax:
document.
setUTCFullYear( yearVal [, monthVal, dayVal])
setUTCHours
Method
This method is used to set the hours for the supplied date according to universal time.
Syntax:
object.
setUTCHours
(hoursVal [, minutesVal, secondsVal, msVal])
setUTCMilliseconds Method
This method is used to set the milliseconds for the supplied date according to universal time. The
millisecondsVal
parameter expects a number between 0 and 999 athough if this is exceeded, the
setMilliseconds
method will automatically increment other values in the
Date
object, e.g. if 1020 is specified, the seconds value is incremented by one and
millisecondsVal
is set to 20.
Syntax:
object.
setUTCMilliseconds
(millisecondsVal)
setUTCMinutes
Method
This method is used to set the minutes for the supplied date according to universal time.
Syntax:
object.
setUTCMinutes
(minutesVal [, secondsVal, msVal])
setUTCMonth
Method
This method is used to set the month for the supplied date according to universal time.
Syntax:
object.
setUTCMonth
(monthVal [, dayVal])
setUTCSeconds
Method
This method is used to set the seconds for the specified date according to universal time.
Syntax:
object.
setUTCSeconds
(secondsVal [, msVal)
toGMTString
Method
This method converts a local date to Greenwich Mean Time.
Syntax:
object.
toGMTString( )
toLocaleString
Method
This method uses the relevant locale's date conventions when converting a date to a string.
Syntax:
object.
toLocaleString( )
toSource Method
This method is used to return the source code that created the specified
Date
object.
Syntax:
object.
toSource( )
toString Method
This method returns a string that represents the
Date
object. This method is automatically called by JavaScript whenever a
Date
object needs to be displayed as text (as with many of the other methods of the
Date
object).
Syntax:
object.
toString( )
toUTCString Method
This method uses the universal time convention when converting a date to a string.
Syntax:
object.
toUTCString( )
UTC Method
This method returns the number of milliseconds from the date in a
Date
object since January 1, 1970 00:00:00 according to universal time. This is a static method of
Date
so the format is always
Date.UTC()
as opposed to
objectName.UTC()
.
Syntax:
Date.
UTC
(year, month, day [, hours, minutes, seconds, ms])
valueOf Method
This method is used to return a primitive value, as a number data type, of the specified
Date
object. This is returned as the number of millisecond since January 1, 1970 00:00:00.
Syntax:
object.
valueOf( )
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information